package web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.propertyeditors.CustomCollectionEditor;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import dao.CommentDAO;
import dao.NewsDAO;
import dao.TagDAO;
import dao.UserDAO;
import beans.Comment;
import beans.News;
import beans.Tag;

public class NewsController extends MultiActionController {

	private UserDAO userDAO;
	private TagDAO tagDAO;
	private CommentDAO commentDAO;

	public void setCommentDAO(CommentDAO commentDAO) {
		this.commentDAO = commentDAO;
	}

	public void setTagDAO(TagDAO tagDAO) {
		this.tagDAO = tagDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	private NewsDAO newsDAO;

	public void setNewsDAO(NewsDAO newsDAO) {
		this.newsDAO = newsDAO;
	}

	@InitBinder
	protected void initBinder(HttpServletRequest request,
			ServletRequestDataBinder binder) throws Exception {

		super.initBinder(request, binder);

		binder.registerCustomEditor(List.class, "tags",
				new CustomCollectionEditor(List.class) {

					@Override
					protected Object convertElement(Object element) {
						Tag tag = new Tag();

						if (element != null) {
							int id = Integer.valueOf(element.toString());
							tag.setId(id);
						}
						return tag;
					}
				});
	}

	@RequestMapping(value = "/listnews.htm")
	public ModelAndView listnews(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();

		// System.out.println(news.getTags().size());
		modelMap.addAttribute("user", userDAO.getLoggedUser());
		modelMap.addAttribute("newsList", newsDAO.DisplayNews());
		modelMap.addAttribute("tagList", tagDAO.GetAllTag());
		return new ModelAndView("listnews", modelMap);
	}

	@RequestMapping(value = "/news.htm")
	public ModelAndView news(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelMap modelMap = new ModelMap();
		int id = Integer.parseInt(ServletRequestUtils
				.getRequiredStringParameter(request, "id"));
		News news = newsDAO.findNewsById(id);
		modelMap.addAttribute("news", news);
		Comment comment = new Comment();
		comment.setUser(userDAO.getLoggedUser());
		comment.setNews(news);
		modelMap.addAttribute("comments", commentDAO.GetCommentsByNewsId(id));
		modelMap.addAttribute("comment", comment);
		// System.out.println(news.getTags().size());
		modelMap.addAttribute("user", userDAO.getLoggedUser());

		return new ModelAndView("news", modelMap);
	}

	@RequestMapping(value = "/addcomment.htm")
	public ModelAndView addcomment(HttpServletRequest request,
			HttpServletResponse response, Comment comment) throws Exception {

		int id = Integer.parseInt(ServletRequestUtils
				.getRequiredStringParameter(request, "news_id"));
		comment.setUser(userDAO.getLoggedUser());
		News news = newsDAO.findNewsById(id);
		comment.setNews(news);
		System.out.println(request.getParameter("content"));
		commentDAO.saveComment(comment);
		return new ModelAndView("redirect:news.htm?id=" + id);
	}
}
